﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Odyssey.Linq;
using SunTzu.Web;

namespace Odyssey.Web.Models
{
    public static class ViewHelper
    {
        public static List<tb_left_navigation> GetSubLeftNavigation(List<tb_left_navigation> navigations, int parentid)
        {
            return navigations.Where(item => item.parent.Value == parentid).OrderBy(item => item.DisplayOrder).ToList();
        }

        public static List<tb_subclass> GetSubClasses(List<tb_subclass> subclasses, int pareintId)
        {
            return subclasses.Where(item => item.parentid == pareintId).ToList();
        }

        public static List<tb_article> GetArticels(List<tb_article> articles, int classid)
        {
            return articles.Where(item => item.ClassId == classid).Take(8).ToList();
        }

        public static string GetSummary(tb_article article)
        {
            if (article == null)
            {
                return string.Empty;
            }
            string content = HTMLHelper.RemovHTML(article.GetContent());
            if (content.Length > 120)
            {
                content = content.Substring(0, 120) + "...";
            }
            return content;
        }
        public static string GetUrl(tb_article article)
        {
            if (article == null)
            {
                return string.Empty;
            }
            if (!article.IsNews && !string.IsNullOrEmpty(article.UrlCode))
            {
                var subClassUrlCode = CacheHelper<tb_subclass>.GetById(article.ClassId).UrlCode;
                var topClassUrlCode = CacheHelper<tb_topclass>.GetById(article.TopClassId).UrlCode;
                if (!string.IsNullOrEmpty(subClassUrlCode) && !string.IsNullOrEmpty(topClassUrlCode))
                {
                    return string.Format("/{0}/{1}/{2}", topClassUrlCode, subClassUrlCode, article.UrlCode);
                }
            }
            return "/article/detail/" + article.Id;
            
        }

        public static string GetUrl(tb_subclass subClass)
        {
            if (subClass == null)
            {
                return string.Empty;
            }
            if (!string.IsNullOrEmpty(subClass.UrlCode))
            {
                var topClassUrlCode = CacheHelper<tb_topclass>.GetById(subClass.TopClassId).UrlCode;
                if (!string.IsNullOrEmpty(topClassUrlCode))
                {
                    return string.Format("/{0}/{1}", topClassUrlCode, subClass.UrlCode);
                }
            }
            return "/article/detail/" + subClass.Id;

        }

        public static string GetProjectType0(this form_project project)
        {
            var result = "";
            if (project.ProjectType0 != null)
            {
                foreach (var item in project.ProjectType0.Split(','))
                {
                    result += ProjectType0Mapping[item] + ",";
                }
            }
            return result.TrimEnd(',');
        }


        public static string GetProjectTypeName(this form_project project)
        {
            var result = "";
            if (project.ProjectType0 != null)
            {
                foreach (var item in project.ProjectType0.Split(','))
                {
                    return ProjectType0Mapping[item];
                }
            }
            return result.TrimEnd(',');
        }

        public static string GetMagazineCircle(this form_media media)
        {
            switch (media.MagazineCircle)
            {
                case 0:
                    return "周刊";
                case 1:
                    return "半月刊";
                case 2:
                    return "月刊";
                case 3:
                    return "季刊";
                default:
                    break;
            }
            return null;
        }

        public static string GetProjectTypeName(string projectType0)
        {
            return ProjectType0Mapping[projectType0];
        }
        private static Dictionary<string, string> ProjectType0Mapping = new Dictionary<string, string>
            { 
                { "0", "企业入驻"},
                { "1", "园区建设"},
                { "2", "项目融资"},
                { "3", "投资项目"},
                { "4", "产品代理"},
                { "5", "加盟合作"},
                { "6", "运营推广"},
                { "7", "设计合作"},
                { "8", "生产开发"},
                { "9", "品牌推广"},
            };
    }
}